package hassion;

import java.io.*;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import mybatis.yuandengta.yuandengta.pojo.User;


/**
 * @author Jim
 * @date 15:27 2022/5/6
 * @description
 * @email:huwmz04153277@126.com
 */
public class Hession {
    public static void main(String[] args) throws Exception {
        User u = new User();
        System.err.println("hession:"+ hessionSeriallize(u).length);
        System.err.println("jdk:"+ jdkSeriallize(u).length);
    }

    public  static <T> byte[] hessionSeriallize(T o) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Hessian2Output output = new Hessian2Output(baos);
        try {
            output.writeObject(o);
            output.flush();
            byte[] bytes = baos.toByteArray();
            return bytes;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                baos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                output.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
    public static Object hessionDeserizlize(byte[] bytes) {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        Hessian2Input input = new Hessian2Input(bais);
        Object o = null;
        try {
            o = input.readObject();
            return o;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                bais.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                input.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }


    }

    public static <T> T hessionDeserizlize(byte[] bytes,Class<T> clazz) {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        Hessian2Input input = new Hessian2Input(bais);
        T o = null;
        try {
            o = (T)input.readObject();
            return o;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                bais.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                input.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }


    }

    public  static byte[] jdkSeriallize(Object o) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream output = new ObjectOutputStream(baos);
        output.writeObject(o);
        output.flush();
        byte[] bytes = baos.toByteArray();
        baos.close();
        output.close();
        return bytes;


    }

    public static <T> T jdkDeserizlize(byte[] bytes) throws Exception {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream input = new ObjectInputStream(bais);
        T o = (T)input.readObject();
        bais.close();
        input.close();
        return o;
    }


}
